//
//  DiscoverCell.m
//  Read History
//
//  Created by Jam on 16/9/1.
//  Copyright © 2016年 Jam. All rights reserved.
//

#import "DiscoverCell.h"

@implementation DiscoverCell
@synthesize nameLabel,readCountLabel,readBtn,icon,iconImageView,rightView,delegate;
@synthesize dataInfo;

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    rightView.layer.cornerRadius = 5;
    rightView.layer.borderWidth = 1;
    rightView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}

-(void)setupData:(NSDictionary *)info
{
    self.dataInfo = info;
    nameLabel.text = [info[@"channel_name"] convertToString];
    [nameLabel sizeToFit];
    icon.frame = CGRectMake(nameLabel.maxX+2, nameLabel.minY+2, 12, 12);
    readCountLabel.text = [NSString stringWithFormat:@"%@万人已订阅",info[@"channel_subscriptioncount"]];
    NSArray *subAry = info[@"is_sub"];
    BOOL isSub = subAry.count > 0;
    [readBtn setTitle:isSub ? @"已阅":@"订阅" forState:UIControlStateNormal];
    readBtn.selected = isSub;
    iconImageView.image = [UIImage imageNamed:isSub?@"nike":@"Home_add_press"];
}

- (IBAction)readBtnClick:(UIButton *)sender {
    if (sender.selected) {
        NSArray *subAry = dataInfo[@"is_sub"];
        if (subAry.count > 0) {
            NSDictionary *item = subAry.firstObject;
            NSDictionary *params = @{@"cs_cid":[[UserEntity sharedUser] getUserID],@"cs_channelid":[dataInfo[@"channel_id"] convertToString],@"cs_id":[item[@"cs_id"] convertToString]};
            [WebService deleteMarkChannel:params WithBlock:^(id result, NSError *error) {
                if (!error) {
                    if ([result[@"errcode"] integerValue] == 0) {
                        sender.selected = !sender.selected;
                        [sender setTitle:@"订阅" forState:UIControlStateNormal];
                        iconImageView.image = [UIImage imageNamed:@"Home_add_press"];
                        if (delegate && [delegate respondsToSelector:@selector(sendValue:)]) {
                            [delegate sendValue:sender.selected];
                        }
                        NSDictionary *dict = @{@"channel_name":dataInfo[@"channel_name"],@"cs_channelid":dataInfo[@"channel_id"],@"is_new":@"y"};
                        [[NSNotificationCenter defaultCenter] postNotificationName:@"deleteUserchannel" object:dict];
                    }
                
                }
                
            }];
        }
    }else {
        NSDictionary *params = @{@"cs_cid":[[UserEntity sharedUser] getUserID],@"cs_channelid":[dataInfo[@"channel_id"] convertToString]};
        [WebService addMarkChannel:params WithBlock:^(id result, NSError *error) {
            if (!error) {
                if ([result[@"errcode"] integerValue] == 0) {
                    sender.selected = !sender.selected;
                    [sender setTitle:@"已阅" forState:UIControlStateNormal];
                    iconImageView.image = [UIImage imageNamed:@"nike"];
                    if (delegate && [delegate respondsToSelector:@selector(sendValue:)]) {
                        [delegate sendValue:sender.selected];
                    }
                    NSDictionary *dict = @{@"channel_name":dataInfo[@"channel_name"],@"cs_channelid":dataInfo[@"channel_id"]};
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"addUserchannel" object:dict];
                }
            }
        }];
    }
}


@end
